const random01 = function() {
    if (Math.random() > 0.5) {
        return 1
    } else {
        return 0
    }
}
const zeroLine = function(n) {
    let result = []
    for (let i = 0; i < n; i++) {
        result.push(0)
    }
    return result
}
const zeroSquare = function(n) {
    let result = []
    for (let i = 0; i < n; i++) {
        result.push(zeroLine(n))
    }
    return result
}

const randomXY = function(n) {
    let x = Math.random() * 100
    let y = Math.random() * 100
    x = Math.floor(x) % n
    y = Math.floor(y) % n
    return [x, y]
}
const arrayEqual = function(arr1, arr2) {
    let s1 = JSON.stringify(arr1)
    let s2 = JSON.stringify(arr2)
    return s1 === s2
}
const uniqueArray = function(arr) {
    let len = arr.length
    for (let i = 0; i < len; i++) {
        for (let j = i + 1; j < len; j++) {
            let e = arrayEqual(arr[i], arr[j])
            if (e) {
                arr.splice(j, 1)
                j = j - 1
                len = len - 1
            }
        }
    }
    return arr
}
const randomXYLine = function(squareLength, n) {
    let result = []
    for (let i = 0; i < n; i++) {
        let c = randomXY(squareLength)
        result.push(c)
        let l1 = result.length
        uniqueArray(result)
        let l2 = result.length
        if (l1 !== l2) {
            i = i - 1
        }
    }
    log('line', result)
    return result
}
const zeroSquareAdd9 = function(array, n) {
    let cs = randomXYLine(array.length, n)
    for (let i = 0; i < cs.length; i++) {
        let c = cs[i]
        let x = c[0]
        let y = c[1]
        array[x][y] = 9
    }
    return array
}
const randomLine01 = function(n) {
    let result = []
    for (let i = 0; i < n; i++) {
        result.push(random01())
    }
    return result
}
const randomSquare01 = function(n) {
    let result = []
    for (let i = 0; i < n; i++) {
        result.push(randomLine01(n))
    }
    return result
}
const randomLine09 = function(n) {
    let result = randomLine01(n)
    for (let i = 0; i < n; i++) {
        if (result[i] === 1) {
            result[i] = 9
        }
    }
    return result
}
const randomSquare09 = function(n) {
    let result = []
    for (let i = 0; i <n ; i++) {
        let line = randomLine09(n)
        result.push(line)
    }
    return result
}

const cloneSquare = function(array) {
    let result = []
    for (let i = 0; i < array.length; i++) {
        result.push(array[i].slice(0))
    }
    return result
}
const markedSquareCell = function(square, i, j) {
    let len = square.length
    let i_j_valid = (i >= 0 && i < len && j >= 0 && j < len)
    if (i_j_valid && square[i][j] !== 9) {
        square[i][j] += 1
    }
    return square
}

const markedAround = function(square, i, j) {
    if (square[i][j] === 9) {
        for (let k = i - 1; k < i + 2; k++) {
            for (let l = j - 1; l < j + 2; l++) {
                markedSquareCell(square, k, l)
            }
        }
    }
    return square
}
const markedSquare = function(array) {
    let square = cloneSquare(array)
    for (let i = 0; i < square.length; i++) {
        for (let j = 0; j < square.length; j++) {
            markedAround(square, i, j)
        }
    }
    return square
}